//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Research.DataLayer;
using System.IO;
using System.Reflection;
using System.Configuration.Install;
using System.Diagnostics;

namespace TridentDbInstaller
{
    class InstallConnectionSettings
    {
        public static bool AddConfiguration(string databaseServer, string databaseName, string userName, string password, int authentication)
        {
            if (String.IsNullOrEmpty(databaseName) || String.IsNullOrEmpty(databaseServer) || (authentication < 1) || authentication > 2)
            {
                return false;
            }

            //Set the alternate path as my location
            Assembly me = Assembly.GetExecutingAssembly();
            string myPath = Path.GetDirectoryName(me.Location);
            ConnectionManager.AlternateProviderLocation = myPath;
            IConnMgrInfoParams sqlParams = null;

            //Create SQL connection 
            if (authentication == (int)TriDbAuthentication.SQLServerAuthentication)
            {
                sqlParams = SQLConnectionParameters.CreateUserPassConnection(databaseName, userName, password, databaseServer, databaseName);
            }
            else if (authentication == (int)TriDbAuthentication.TrustedAuthentication)
            {
                sqlParams = SQLConnectionParameters.CreateTrustedConnection(databaseName, databaseServer, databaseName);
            }

            AddServiceConfiguration(sqlParams);
            AddComposerConfiguration(sqlParams);
            AddWebServiceConfiguration(sqlParams);

            return true;
        }
        private static bool AddServiceConfiguration(IConnMgrInfoParams sqlParams)
        {
            ConnectionManager cmgr = ConnectionManager.CreateForAgent(ConnectionManager.CancelBehavior.SilentlyReturnNull,
                                            ConnectionManager.Agent.ExecutionService);

            //Create the empty store
            cmgr.CreateEmptyStore();
            cmgr.AddConnection(sqlParams);
            cmgr.DefaultConnection = sqlParams;

            return true;

        }

        private static bool AddWebServiceConfiguration(IConnMgrInfoParams sqlParams)
        {
            ConnectionManager cmgr = ConnectionManager.CreateForAgent(ConnectionManager.CancelBehavior.SilentlyReturnNull,
                                            ConnectionManager.Agent.WebService);

            //Create the empty store
            cmgr.CreateEmptyStore();
            cmgr.AddConnection(sqlParams);
            cmgr.DefaultConnection = sqlParams;

            return true;

        }
        /// <summary>
        /// Adds the composer configuration to 
        /// </summary>
        /// <param name="databaseServer"></param>
        /// <param name="databaseName"></param>
        /// <returns></returns>
        private static bool AddComposerConfiguration(IConnMgrInfoParams sqlParams)
        {
            ConnectionManager cmgr = ConnectionManager.Create(ConnectionManager.CancelBehavior.SilentlyReturnNull, ConnectionManagerBase.ConfigScope.Template);
            //Create the empty store
            cmgr.CreateEmptyStore();
            cmgr.AddConnection(sqlParams);
            //set this connection as default
            cmgr.DefaultConnection = sqlParams;
            return true;
        }
    }
}
